package com.feisuanyz.service.impl;

import com.feisuanyz.dto.TicketManagementDTO;
import com.feisuanyz.entity.TicketManagement;
import com.feisuanyz.query.TicketManagementQuery;
import com.feisuanyz.repository.TicketManagementRepository;
import com.feisuanyz.repository.UserPermissionRepository;
import com.feisuanyz.request.TicketUpdateRequest;
import com.feisuanyz.response.RestResult;
import com.feisuanyz.service.TicketManagementService;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class TicketManagementServiceImpl implements TicketManagementService {

    @Autowired
    private TicketManagementRepository ticketManagementRepository;

    @Autowired
    private UserPermissionRepository userPermissionRepository;

    @Autowired
    private ModelMapper modelMapper;

    @Override
    public RestResult<TicketManagementDTO> getTicketInfo(TicketManagementQuery query) {
        Optional<TicketManagement> ticketOptional = ticketManagementRepository.findByEventId(query.getEventId());
        if (ticketOptional.isPresent()) {
            TicketManagement ticket = ticketOptional.get();
            TicketManagementDTO ticketDTO = modelMapper.map(ticket, TicketManagementDTO.class);
            return new RestResult<>("000000", "调用成功", ticketDTO);
        } else {
            return new RestResult<>("000001", "事件ID不存在", null);
        }
    }

    @Override
    @Transactional
    public RestResult<Void> updateAvailableTickets(TicketUpdateRequest request, Integer userId) {
        if (!hasEditPermission(userId)) {
            return new RestResult<>("000001", "无权限编辑", null);
        }
        Optional<TicketManagement> ticketOptional = ticketManagementRepository.findById(request.getTicketId());
        if (ticketOptional.isPresent()) {
            TicketManagement ticket = ticketOptional.get();
            ticket.setAvailableTickets(request.getAvailableTickets());
            ticket.setUpdateBy(userId);
            ticketManagementRepository.save(ticket);
            return new RestResult<>("000000", "调用成功", null);
        } else {
            return new RestResult<>("000001", "票务ID不存在", null);
        }
    }

    @Override
    @Transactional
    public RestResult<Void> updateRemainingTickets(TicketUpdateRequest request, Integer userId) {
        if (!hasEditPermission(userId)) {
            return new RestResult<>("000001", "无权限编辑", null);
        }
        Optional<TicketManagement> ticketOptional = ticketManagementRepository.findById(request.getTicketId());
        if (ticketOptional.isPresent()) {
            TicketManagement ticket = ticketOptional.get();
            ticket.setRemainingTickets(request.getRemainingTickets());
            ticket.setUpdateBy(userId);
            ticketManagementRepository.save(ticket);
            return new RestResult<>("000000", "调用成功", null);
        } else {
            return new RestResult<>("000001", "票务ID不存在", null);
        }
    }

    private boolean hasEditPermission(Integer userId) {
        return userPermissionRepository.existsByUserIdAndPermissionLevel(userId, "editor");
    }
}